<template>
	<view>
		<view class="search-container" @click="gotoSearch">
			<my-search></my-search>
		</view>
		<!-- 轮播图 -->
		<swiper indicator-dots autoplay :interval="3000" :duration="1000" circular>
			<swiper-item v-for="item in SwiperData" :key="item.goods_id">
				<navigator class="swiper-item" :url="'/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id">
					<image :src="item.image_src"></image>
				</navigator>
			</swiper-item>
		</swiper>

		<!-- 导航 -->
		<view class="nav-list">
			<view class="nav-item" v-for="(item,index) in CatItems" :key="index" @click="navClickHandler(item)">
				<image :src="item.image_src"></image>
			</view>
		</view>

		<!-- 楼层区域 -->
		<view class="floor-list">
			<view class="floor-item" v-for="(item,i) in FloorList" :key="i">
				<!-- 楼层标题 -->
				<image :src="item.floor_title.image_src" class="floor-title"></image>
				<!-- 楼层图片内容 -->
				<view class="floor-img-box">

					<navigator class="left-box" :url="item.product_list[0].url">
						<image :src="item.product_list[0].image_src"
							:style="{width: item.product_list[0].image_width + 'rpx'}" mode="widthFix"></image>
					</navigator>

					<view class="right-box">
						<navigator v-for="(item2,i2) in item.product_list" v-if="i2 !== 0" :key="i2" :url="item2.url">
							<image :src="item2.image_src" mode="widthFix" :style="{width: item2.image_width + 'rpx'}">
							</image>
						</navigator>
					</view>
				</view>
			</view>
		</view>

	</view>

</template>

<script>
	import badgeMix from "@/mixins/tab-bar-badge.js"
	export default {
		data() {
			return {
				SwiperData: [],
				CatItems: [],
				FloorList: []
			};
		},
		methods: {
			async getSwiperData() {
				const url = 'api/public/v1/home/swiperdata'
				const {
					data
				} = await uni.$http.get(url)
				if (data.meta.status != 200) {
					return uni.$showMsg()
				}
				this.SwiperData = data.message
			},

			async getCatItems() {
				const url = 'api/public/v1/home/catitems'
				const {
					data
				} = await uni.$http.get(url)
				if (data.meta.status !== 200)
					return uni.$showMsg()
				this.CatItems = data.message
			},

			navClickHandler(item) {
				if (item.name === '分类')
					uni.switchTab({
						url: "../Cate/Cate"
					})
			},

			async getFloorList() {
				const url = "api/public/v1/home/floordata"
				const {
					data
				} = await uni.$http.get(url)
				if (data.meta.status !== 200)
					return uni.$showMsg()

				// 处理跳转的url
				data.message.forEach(element => {
					element.product_list.forEach(prod => {
						prod.url =
							`/subpkg/goods_list/goods_list?${prod.navigator_url.split('?')[1]}`
					})
				})

				this.FloorList = data.message
			},

			gotoSearch() {
				uni.navigateTo({
					url: "/subpkg/search/search"
				})
			}
		},
		onLoad() {
			this.getSwiperData()
			this.getCatItems()
			this.getFloorList()
		},
		mixins: [badgeMix],
	}
</script>

<style lang="less" scoped>
	.search-container {
		position: sticky;
		top: 0;
		z-index: 999;
	}


	.swiper-item {
		height: 350rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.nav-list {
		width: 100%;
		display: flex;
		justify-content: space-around;
		margin: 15px 0;

		.nav-item {
			image {
				width: 128rpx;
				height: 140rpx;
			}
		}
	}

	.floor-list {
		.floor-title {
			height: 60rpx;
			width: 100%;
			display: flex;
		}

		.right-box {
			display: flex;
			flex-wrap: wrap;
			justify-content: space-around;
		}

		.floor-img-box {
			display: flex;
			padding-left: 10rpx;
		}
	}
</style>
